/**
 * 
 */
package com.ekingstar.framework.action;

import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.config.Action;

import com.opensymphony.xwork2.ActionSupport;
import com.ekingstar.framework.app.ApplicationParameter;
import com.ekingstar.framework.domain.Role;
import com.ekingstar.framework.domain.User;
import com.ekingstar.framework.security.UserUtil;
import com.ekingstar.framework.service.IRoleService;
import com.ekingstar.framework.service.IUserService;
import com.ekingstar.framework.util.Page;

/**
 * 
 *
 */

@SuppressWarnings("serial")
@Action(name="grantMyRight",namespace="/user")
public class GrantMyRightAction extends ActionSupport{
	
	private IUserService userService;
	private IRoleService roleService;
	private User user;
	private int pageNo = 1;
	public int getPageNo() {
		return pageNo;
	}

	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}


	private int pageSize = ApplicationParameter.INSTANCE.getPageSize();
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public void setRoleService(IRoleService roleService) {
		this.roleService = roleService;
	}

	public void setUserService(IUserService userService) {
		this.userService = userService;
	}
	
	
	private String roleId;
	private List<User> users;
	private Role role;
	public Role getRole() {
		return role;
	}

	public void setRole(Role role) {
		this.role = role;
	}


	private Boolean canUse;
	private Boolean canGrant;
	public Boolean getCanUse() {
		return canUse;
	}

	public void setCanUse(Boolean canUse) {
		this.canUse = canUse;
	}

	public Boolean getCanGrant() {
		return canGrant;
	}

	public void setCanGrant(Boolean canGrant) {
		this.canGrant = canGrant;
	}

	public String execute(){
		role = roleService.viewRole(roleId);
		this.users = userService.getGrantUsers(UserUtil.getCurrentUserId(), roleId);
		return "list";
	}
	/*public String listQueryUserPage(){
		this.page = userService.getUserlistByUser(user,pageNo, pageSize);
		return "list";
	}*/
	
	public String input(){
		role = roleService.viewRole(roleId);
		return "input";
	}
	
	public String invokeMyRight(){
		userService.invokeMyRightFromUser(UserUtil.getCurrentUserId(), roleId, canUse, canGrant, user.getId());
		this.users = userService.getGrantUsers(UserUtil.getCurrentUserId(), roleId);
		return "list";
	}
	public String grantMyRight(){
		role = roleService.viewRole(roleId);
		userService.grantMyRightToUsers(UserUtil.getCurrentUserId(), roleId,canUse,canGrant,users);
		this.users = userService.getGrantUsers(UserUtil.getCurrentUserId(), roleId);
		return "list";
		
	}
	
	public String searchUser(){
		try{
			Page page = userService.getUserlist(user, pageNo, pageSize);
			JSONObject jo = JSONObject.fromObject(page);
			HttpServletResponse res =  ServletActionContext.getResponse();
			//res.setCharacterEncoding("UTF-8");
			res.setContentType("text/paint; charset=UTF-8");
			PrintWriter out = res.getWriter();
			String  jsonString = jo.toString();
			System.out.println(jsonString);
			out.print(jsonString);
		}catch(Throwable e){
			e.printStackTrace();
		}
		return null;
	}

	public String getRoleId() {
		return roleId;
	}

	public void setRoleId(String roleId) {
		this.roleId = roleId;
	}

	public List getUsers() {
		return users;
	}

	public void setUsers(List users) {
		this.users = users;
	}
	
	
	
	

}
